<?php
if(!defined('BLOG'))exit('不作死就不会死');
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/1/12
 * Time: 20:05
 */
/**
 * 截取UTF-8编码下字符串的函数 同时这个函数也被smarty注册
 *
 * @param   string      $str        被截取的字符串
 * @param   int         $length     截取的长度
 * @param   bool        $append     是否附加省略号
 *
 * @return  string
 */
function smarty_modifier_mysubstr($str,$length,$append=true)
{
    $str = trim($str);  // 去除空格
    $strlength = strlen($str);  //统计字符串长度

    if ($length == 0 || $length >= $strlength)
    {
        return $str;
    }
    elseif ($length < 0)
    {
        $length = $strlength + $length;
        if ($length < 0)
        {
            $length = $strlength;
        }
    }

    if (function_exists('mb_substr'))
    {
        $newstr = mb_substr($str, 0, $length, CHARSET);
    }
    elseif (function_exists('iconv_substr'))
    {
        $newstr = iconv_substr($str, 0, $length,CHARSET);
    }
    else
    {
        //$newstr = trim_right(substr($str, 0, $length));
        $newstr = substr($str, 0, $length);
    }

    if ($append && $str != $newstr)
    {
        $newstr .= '...';
    }

    return $newstr;
}


function createtree($comment){
    $arrp=array();
    $arrc=array();
    foreach ($comment as $key=>$val){
        if($val['pid']==0){
            $arrp[]=$val;
        }else{
            $arrc[]=$val;
        }
    }
    foreach ($arrp as $pk=>$pv){
        foreach ($arrc as $ck=>$cv){
            if ($cv['pid']==$pv['id']){
                $arrp[$pk]['childs'][]=$cv;
            }
        }
    }
    return $arrp;
}
